<?
# -------------------------------------------------------------------------------------
# phyoCalendar.class.php - Feb 2004
# version: 1.0
# Author: pazu@phyo.net
# Licence: Distributed under GPL
# 
# You are free to modify and/re-distribute this software without my consent.
# I'll be happy to answer questions.
#
# calendar class library
# return monthly calender as an array containing dates formated in a matrix
# successfully splits format and structure.
# -------------------------------------------------------------------------------------

class Calendar
{
	var $year; // the year of interest
	var $month; // the month of interest
	var $numdays; // number of days in given month
	
	/*
	Calendar function
	- initialises vars
	*/
	function Calendar($m=false, $y=false) {
		# this function is used to initialise values	
		$this->setYear($y);
		$this->setMonth($m);
	}
    
    /*
    setMonth function
    - sets global month of interest
    */
	function setMonth($m=false) {
		if (!$m) {
			$this->month = date("m");		
		} else {
			$this->month = $m;
		}		
		$this->numdays = $this->getDaysInMonth();
		return true;		
	}

	/* 
	setYear function
	- sets global year of interest
	*/
	function setYear($y=false) {
		if (!$y) {
			$this->year = date("Y");		
		} else {
			$this->year = $y;
		}
		return true;
	}
	
	/*
	getCalendarMonth function
	- retrieve matrix of days with in month of interest
	- either format output or return timestamps
	- returns matrix (weekday) by (week of year)
	*/
	function getCalendarMonth($format=false) {
		for ($i=1; $i<=$this->numdays; $i++) {
			$wday = date("D", mktime(0, 0, 0, $this->month, $i, $this->year)); // textual day of week e.g. Sun
			$week = date("W", mktime(0, 0, 0, $this->month, $i, $this->year)); // week of year
			if (!$format) {
				$month[$week][$wday] = mktime(0, 0, 0, $this->month, $i, $this->year);
			} else {
				$month[$week][$wday] = date($format, mktime(0, 0, 0, $this->month, $i, $this->year));
			}
		}
		return $month;
	}
	
	/*
	getCalendarYear
	- retrieves monthly calendars for the whole year
	*/
	function getCalendarYear($format=false) {
		for ($i=0; $i < 12; $i++) {
			$this->setMonth($i);
			$year[$i] = $this->getCalendarMonth($format);
		}
		return $year;
	}
	
	/*
	getDayInMonth
	- retrieves the number of days in this month
	- e.g, a leap year Feb has 29 days so getDaysInMonth returns 29
	*/
	function getDaysInMonth() {
		return date("t", mktime(0, 0, 0, $this->month,01,$this->year));
	}
	
	/*
	getNextMonth
	*/
	function getNextMonth($format=false) {
		if (!$format) {
			return mktime(0, 0, 0, $this->month+1,01,$this->year);
		} else {
			return date($format, mktime(0, 0, 0, $this->month+1,01,$this->year));
		}
	}

	/*
	getLastMonth
	*/	
	function getLastMonth($format=false) {
		if (!$format) {
			return mktime(0, 0, 0, $this->month,0,$this->year);
		} else {
			return date($format, mktime(0, 0, 0, $this->month,0,$this->year));
		}
	}

	function getTimestamp($day)	{
		if(!$day)
		return "";
		else return mktime(0, 0, 0, $this->month,$day,$this->year);
	}
}
?>